﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Model;
using EQT.Dal;
using System.Collections.Generic;
using Common.Logging;

/// <summary>
/// 产品检测能力维护。
/// </summary>
public partial class TestItem : System.Web.UI.Page
{
    private DaProductCheckAbility daProduct = DevFuture.Common.WorkContext.AppCtx.GetObject("DaProductCheckAbility") as DaProductCheckAbility;
    private static readonly ILog log = LogManager.GetCurrentClassLogger();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        divResult.Visible = false;
        OperationResult.Text = "";

        if (!Page.IsPostBack)
        {
            this.SetPanelVisiable(mainPanel);
            this.BindProductTree();
        }
    }

    protected void btnCreateItem_Click(object sender, EventArgs e)
    {
        ddlCreatePName.DataSource = daProduct.GetDistinctProduct();
        ddlCreatePName.DataBind();
        if (ddlCreatePName.Items.Count > 0)
        {
            this.ddlCreatePName_SelectedIndexChanged(null, null);
        }
        this.SetPanelVisiable(createPanel);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        mainPanel.Visible = false;
        createPanel.Visible = false;
        editPanel.Visible = false;

        panel.Visible = true;
    }

    protected void btnCreateCancel_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(mainPanel);
    }

    protected void btnEditCancel_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(mainPanel);
    }

    /// <summary>
    /// 产品树绑定
    /// </summary>
    protected void BindProductTree()
    {
        productTreeView.Nodes.Clear();
        DataSet dsProduct = daProduct.GetDistinctProduct();

        if (dsProduct != null)
        {
            for (int i = 0; i < dsProduct.Tables[0].Rows.Count; i++)
            {
                TreeNode tnRoot = new TreeNode(dsProduct.Tables[0].Rows[i]["PName"].ToString(), dsProduct.Tables[0].Rows[i]["PName"].ToString());

                this.BindModelNode(tnRoot, dsProduct.Tables[0].Rows[i]["PName"].ToString());

                productTreeView.Nodes.Add(tnRoot);
            }
        }
        productTreeView.ExpandAll();
    }

    /// <summary>
    /// 型号节点绑定
    /// </summary>
    protected void BindModelNode(TreeNode tnParent, string pname)
    {
        DataSet ds = daProduct.GetDistinctModel(pname);

        if (ds.Tables[0].Rows.Count == 0)
            return;

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            TreeNode tn = new TreeNode(ds.Tables[0].Rows[i]["PModel"].ToString(), ds.Tables[0].Rows[i]["PModel"].ToString());
            tnParent.ChildNodes.Add(tn);
        }
    }

    /// <summary>
    /// 参数绑定。
    /// </summary>
    protected void BindTypeList(string pname,string pmodel)
    {
        repProductType.DataSource = daProduct.GetProductType(pname,pmodel);
        repProductType.DataBind();
    }

    protected void productTreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        
        switch (productTreeView.SelectedNode.Depth)
        { 
            case 0:
                repProductType.DataSource = null;
                repProductType.DataBind();
                labNowProduct.Text = productTreeView.SelectedNode.Text;
                break;
            case 1:
                repProductType.DataSource = daProduct.GetProductType(productTreeView.SelectedNode.Parent.Value,productTreeView.SelectedNode.Value);
                repProductType.DataBind();
                labNowProduct.Text =productTreeView.SelectedNode.Parent.Value+" > "+ productTreeView.SelectedNode.Text;
                break;
            default:
                return;
        }
    }

    /// <summary>
    /// 创建保存。
    /// </summary>
    protected void btnCreateSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtCreatePType.Text.Trim()))
        {
            OperationResult.Text = "请输入参数名称！"; divResult.Visible = true; return;
        }
        if (string.IsNullOrEmpty(txtCreateGuidePrice.Text.Trim()))
        {
            OperationResult.Text = "请输入参考价格！"; divResult.Visible = true; return;
        }
        else
        {
            decimal decOut;
            if (!decimal.TryParse(txtCreateGuidePrice.Text.Trim(),out decOut))
            {
                OperationResult.Text = "请输入正确的参考价格！"; divResult.Visible = true; return;
            }
        }
        if (ckbIsNewPName.Checked&&string.IsNullOrEmpty(txtCreatePName.Text.Trim()))
        {
            OperationResult.Text = "请输入产品名称！"; divResult.Visible = true; return;
        }

        if (ckbIsNewPModel.Checked && string.IsNullOrEmpty(txtCreatePModel.Text.Trim()))
        {
            OperationResult.Text = "请输入型号名称！"; divResult.Visible = true; return;
        }

        if (!ckbIsNewPName.Checked && string.IsNullOrEmpty(ddlCreatePName.SelectedValue))
        {
            OperationResult.Text = "请选择产品名称！"; divResult.Visible = true; return;
        }

        if (!ckbIsNewPModel.Checked && string.IsNullOrEmpty(ddlCreatePModel.SelectedValue))
        {
            OperationResult.Text = "请选择型号名称！"; divResult.Visible = true; return;
        }


        MoProductCheckAbility mod = new MoProductCheckAbility();
        mod.PcaId = SequenceKeys.GenerateProductCheckAbility();
        mod.PcaState = 0;
        mod.Pmodel =ckbIsNewPModel.Checked? txtCreatePModel.Text.Trim():ddlCreatePModel.SelectedValue;
        mod.Pname =ckbIsNewPName.Checked? txtCreatePName.Text.Trim():ddlCreatePName.SelectedValue;
        mod.Ptype = txtCreatePType.Text.Trim();
        mod.Rem = txtCreateRem.Text.Trim();
        mod.TltDocDdl = "";
        mod.TltDocDrl = "";
        mod.TltDocReport = "";
        mod.TltXlsDdl = "";
        mod.TltXlsDrl = "";
        mod.TltXlsPtype = "";
        mod.Class1 = "";
        mod.CreateTime = DateTime.Now;
        mod.GuidePrice = decimal.Parse(txtCreateGuidePrice.Text.Trim());

        try
        {
            int rtn=daProduct.Add(mod);
            if (rtn == 1)
            {
                this.BindProductTree();
                this.BindTypeList(mod.Pname,mod.Ptype);
                OperationResult.Text = "产品检验能力项创建成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "产品检验能力项创建失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("产品检验能力项创建失败", ex);
            OperationResult.Text = "产品检验能力项创建失败！"; divResult.Visible = true;
        }

    }

    /// <summary>
    /// 编辑保存。
    /// </summary>
    protected void btnEditSave_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txtEditPType.Text.Trim()))
        {
            OperationResult.Text = "请输入参数名称！"; divResult.Visible = true; return;
        }
        if (string.IsNullOrEmpty(txtEditGuidePrice.Text.Trim()))
        {
            OperationResult.Text = "请输入参考价格！"; divResult.Visible = true; return;
        }
        else
        {
            decimal decOut;
            if (!decimal.TryParse(txtEditGuidePrice.Text.Trim(),out decOut))
            {
                OperationResult.Text = "请输入正确的参考价格！"; divResult.Visible = true; return;
            }
        }

        MoProductCheckAbility mod = daProduct.GetEntities("")[0];
        mod.Ptype = txtEditPType.Text.Trim();
        mod.Rem = txtEditRem.Text.Trim();
        mod.GuidePrice = decimal.Parse(txtEditGuidePrice.Text.Trim());

        try
        {
            int rtn = daProduct.Update(mod);
            if (rtn == 1)
            {
                this.BindTypeList(mod.Pname, mod.Ptype);
                OperationResult.Text = "产品检验能力项修改成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "产品检验能力项修改失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("产品检验能力项修改失败", ex);
            OperationResult.Text = "产品检验能力项修改失败！"; divResult.Visible = true;
        }
    }

    protected void repProductType_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            switch (e.CommandName)
            { 
                case "Edit":
                    #region 编辑
                    MoProductCheckAbility mod = daProduct.GetEntities(" where PCA_ID='"+e.CommandArgument.ToString()+"'")[0];
                    txtEditRem.Text= mod.Rem;
                    txtEditGuidePrice.Text=mod.GuidePrice.ToString();
                    labEditID.Text = mod.PcaId;
                    labEditPModel.Text = mod.Pmodel;
                    labEditPName.Text = mod.Pname;
                    txtEditPType.Text = mod.Ptype;

                    this.SetPanelVisiable(editPanel);
                    #endregion
                    break;

                case "Delete":
                    #region 删除
                    try
                    {
                        if (daProduct.DeleteByWhereClause(" where pca_id='" + e.CommandArgument.ToString() + "'") == 1)
                        {
                            this.BindTypeList(productTreeView.SelectedNode.Parent.Value, productTreeView.SelectedNode.Value);
                            OperationResult.Text = "产品检验能力项删除成功！"; divResult.Visible = true;
                        }
                        else
                        {
                            OperationResult.Text = "产品检验能力项删除失败！"; divResult.Visible = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error("产品检验能力项删除失败", ex);
                        OperationResult.Text = "产品检验能力项删除失败！"; divResult.Visible = true;
                    }

                    #endregion
                    break;
                case "cmd1":
                    daProduct.RemoveTemplateFile(e.CommandArgument.ToString(), 1);
                    btnRefresh_Click(null, null);
                    break;
                case "cmd2":
                    daProduct.RemoveTemplateFile(e.CommandArgument.ToString(), 2);
                    btnRefresh_Click(null, null);
                    break;
                case "cmd3":
                    daProduct.RemoveTemplateFile(e.CommandArgument.ToString(), 3);
                    btnRefresh_Click(null, null);
                    break;
                case "cmd4":
                    daProduct.RemoveTemplateFile(e.CommandArgument.ToString(), 4);
                    btnRefresh_Click(null, null);
                    break;
                case "cmd5":
                    daProduct.RemoveTemplateFile(e.CommandArgument.ToString(), 5);
                    btnRefresh_Click(null, null);
                    break;
                case "cmd6":
                    daProduct.RemoveTemplateFile(e.CommandArgument.ToString(), 6);
                    btnRefresh_Click(null, null);
                    break;
                default:
                    return;
            }
        }
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        //这里隐藏刷新。
        this.BindTypeList(productTreeView.SelectedNode.Parent.Value, productTreeView.SelectedNode.Value);
    }

    protected void ddlCreatePName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ckbIsNewPModel.Checked)
        {
            ddlCreatePModel.Items.Clear();
            ddlCreatePModel.DataSource = daProduct.GetDistinctModel(ddlCreatePName.SelectedValue);
            ddlCreatePModel.DataBind();
        }
        else
        {
            ddlCreatePModel.Attributes.Add("style", "display:none;");
            txtCreatePModel.Attributes.Add("style", "display:;");
        }
    }
}
